package Servlet;

import DAO.*;
import javabean.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/Complaint")
public class ComplaintServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();
        Customer customer = (Customer) session.getAttribute("loginCustomer");
        OrderDAO orderDAO = (OrderDAO) DAOFactory.getOrderDAOInstance();
        Order order = new Order();
        order.setOrderID(Integer.parseInt(req.getParameter("order-id")));
        try {
            order = orderDAO.getOrder(order);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (order == null) {
            PrintWriter out = resp.getWriter();
            out.print("<script>alert('您所投诉的订单不存在!');window.location.href='MyComplaint';</script>");
            return;
        } else if(order.getCustomerID()!=customer.getCustomerID()){
            PrintWriter out = resp.getWriter();
            out.print("<script>alert('您所投诉的订单不属于您!');window.location.href='MyComplaint';</script>");
            return;
        }
        if (customer == null) {
            resp.sendRedirect("index.jsp"); // 如果客户未登录，则重定向到登录页面
            return;
        }

        // 获取投诉信息
        //TODO 当前订单号应该从哪里获取？无效的订单号会怎么样？尚未解决
        String orderID = req.getParameter("order-id");
        String complaintType = req.getParameter("complaint-type");
        String complaintContent = req.getParameter("complaint-content");


        // 创建投诉对象
        Complaint complaint = new Complaint();
        complaint.setOrderID(Integer.parseInt(orderID));
        complaint.setCustomerID(customer.getCustomerID());
        complaint.setComplaintDescription(complaintContent);

        // 添加投诉到未处理投诉表中
        try {
            IComplaintDAO complaintDAO = DAOFactory.getComplaintDAOInstance();
            boolean isSuccess = complaintDAO.addUnassignedComplaint(complaint);
            PrintWriter out = resp.getWriter();
            if (isSuccess) {
                // 投诉成功
                out.print("<script>alert('投诉成功!');window.location.href='MyComplaint';</script>");
            } else {
                // 投诉失败
                out.print("<script>alert('投诉失败!');window.location.href='MyComplaint';</script>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
